package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping(value = "/manage/category/")
public class  CategoryManageController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private ICategoryService iCategoryService;

    @PostMapping(value = "add_category.do")
    public ServerResponse addCategory(HttpSession session, String categoryname, @RequestParam(value = "parentId",defaultValue = "0")Integer parentId){

        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            ServerResponse.createdByErrorMessage("用户未登陆，请登陆");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //是管理员
            //增加处理分类逻辑
            return iCategoryService.addCategory(categoryname,parentId);
        }else{
            return ServerResponse.createdByErrorMessage("无权限操作，需要管理员权限");
        }
    }
    @PostMapping(value = "update_category_name.do")
    public ServerResponse updateCategoryName(Integer categoryId,String categoryName){
        return iCategoryService.updateCategoryName(categoryId,categoryName);
    }
    @PostMapping(value = "get_category.do")
    public  ServerResponse getChildParalleCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            ServerResponse.createdByErrorMessage("用户未登陆，请登陆");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            return iCategoryService.getChildParalleCategory(categoryId);
        }else {
            return ServerResponse.createdByErrorMessage("无权限操作，需要管理员权限");        }

    }
    @PostMapping(value = "get_deep_category.do")
    public  ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            ServerResponse.createdByErrorMessage("用户未登陆，请登陆");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //查询递归子节点
            return iCategoryService.selectCategoryAndChildrenById(categoryId);
        }else {
            return ServerResponse.createdByErrorMessage("无权限操作，需要管理员权限");        }

    }
}
